package com.fangzhi.controller;

import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.mobel.io.SaveCustomerIo;
import com.fangzhi.mobel.vo.DeptListVo;
import com.fangzhi.mobel.vo.LoginVo;
import com.fangzhi.mobel.vo.RoleListVo;
import com.fangzhi.service.AdminAccountInfoService;
import com.fangzhi.service.CustomerService;
import com.fangzhi.service.DeptBaseInfoService;
import com.fangzhi.service.RoleBaseInfoService;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private RoleBaseInfoService roleBaseInfoService;
    @Autowired
    private DeptBaseInfoService deptBaseInfoService;
    @Autowired
    private AdminAccountInfoService adminAccountInfoService;

    /**
     * 新增客户
     * @return
     */
    @PostMapping("/saveCustomer")
    public ResponseResult saveCustomer(@RequestBody SaveCustomerIo io){
        return customerService.saveCustomer(io);
    }

    /**
     * 下线客户
     * @return
     */
    @GetMapping("/offlineCustomer")
    public ResponseResult offlineCustomer(@RequestParam Integer id){
        return customerService.offlineCustomer(id);
    }

    /**
     * 客户列表
     * @return
     */
    @GetMapping("/customerList")
    public ResponseResult customerList(@RequestParam(defaultValue = "0") Integer roleId
                                    , @RequestParam(defaultValue = "0") Integer deptId
                                    , @RequestParam(defaultValue = "0") Integer accountId
                                    , @RequestParam(defaultValue = "") String keyword
                                    , @RequestParam(defaultValue = "1") Integer pageNo
                                    , @RequestParam(defaultValue = "20") Integer pageSize){
        List<RoleListVo> roleListVos = roleBaseInfoService.roleList(roleId);
        List<DeptListVo> deptListVos = deptBaseInfoService.deptList(deptId);
        if (roleListVos.isEmpty() || deptListVos.isEmpty())
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);

        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        if (loginVo.getRoleId() == 3 || loginVo.getRoleId() == 2  || loginVo.getRoleId() == 4)
            accountId = loginVo.getId().intValue();

        List<Long> roleIds = roleListVos.stream().map(T -> T.getId()).collect(Collectors.toList());
        List<Long> deptIds = deptListVos.stream().map(T -> T.getId()).collect(Collectors.toList());
        List<Long> accountIds = adminAccountInfoService.accountList(roleIds, deptIds, accountId);
        if (accountIds.isEmpty())
            return ResponseResult.OK(new ArrayList<>());
        return customerService.customerList(accountIds, keyword, pageNo, pageSize);
    }
}
